using Microsoft.EntityFrameworkCore;
using RiceShop.Models;

namespace RiceShop.Services
{
    /// <summary>
    /// 品牌服务接口
    /// </summary>
    public interface IBrandService
    {
        /// <summary>
        /// 获取所有品牌列表
        /// </summary>
        Task<IEnumerable<Brand>> GetBrandsAsync();

        /// <summary>
        /// 根据ID获取品牌详情
        /// </summary>
        /// <param name="id">品牌ID</param>
        Task<Brand?> GetBrandByIdAsync(long id);

        /// <summary>
        /// 创建新品牌
        /// </summary>
        /// <param name="brand">品牌信息</param>
        Task<Brand> CreateBrandAsync(Brand brand);

        /// <summary>
        /// 更新品牌信息
        /// </summary>
        /// <param name="brand">更新的品牌信息</param>
        Task<bool> UpdateBrandAsync(Brand brand);

        /// <summary>
        /// 删除品牌
        /// </summary>
        /// <param name="id">品牌ID</param>
        Task<bool> DeleteBrandAsync(long id);
    }

    /// <summary>
    /// 品牌服务实现类
    /// </summary>
    public class BrandService : IBrandService
    {
        private readonly ApplicationDbContext _context;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="context">数据库上下文</param>
        public BrandService(ApplicationDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取所有未删除的品牌列表，按排序号排序
        /// </summary>
        public async Task<IEnumerable<Brand>> GetBrandsAsync()
        {
            return await _context.Brands
                .Where(b => b.IsDeleted == false)
                .OrderBy(b => b.Sort)
                .ToListAsync();
        }

        /// <summary>
        /// 根据ID获取未删除的品牌详情
        /// </summary>
        /// <param name="id">品牌ID</param>
        public async Task<Brand?> GetBrandByIdAsync(long id)
        {
            return await _context.Brands
                .FirstOrDefaultAsync(b => b.Id == id && b.IsDeleted == false);
        }

        /// <summary>
        /// 创建新品牌
        /// </summary>
        /// <param name="brand">品牌信息</param>
        public async Task<Brand> CreateBrandAsync(Brand brand)
        {
            _context.Brands.Add(brand);
            await _context.SaveChangesAsync();
            return brand;
        }

        /// <summary>
        /// 更新品牌信息
        /// </summary>
        /// <param name="brand">更新的品牌信息</param>
        /// <returns>更新是否成功</returns>
        public async Task<bool> UpdateBrandAsync(Brand brand)
        {
            var existingBrand = await _context.Brands
                .FirstOrDefaultAsync(b => b.Id == brand.Id);

            if (existingBrand == null)
                return false;

            _context.Entry(existingBrand).CurrentValues.SetValues(brand);
            await _context.SaveChangesAsync();
            return true;
        }

        /// <summary>
        /// 软删除品牌（将IsDeleted标记为true）
        /// </summary>
        /// <param name="id">品牌ID</param>
        /// <returns>删除是否成功</returns>
        public async Task<bool> DeleteBrandAsync(long id)
        {
            var brand = await _context.Brands
                .FirstOrDefaultAsync(b => b.Id == id);

            if (brand == null)
                return false;

            brand.IsDeleted = true;
            brand.UpdateTime = DateTime.Now;
            await _context.SaveChangesAsync();
            return true;
        }
    }
}